package com.hsav.kcxb.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hsav.kcxb.R;
import com.hsav.kcxb.activity.BaseActivity;
import com.hsav.kcxb.bean.MenuModel;

import java.util.List;

/**
 * Created by zhao on 17-4-30.
 */

public class MenuAdpater extends BaseAdapter {

    private final Context mContext;
    private final List<MenuModel> mMenuModeList;
    private final boolean mIsLeft;
    private final LayoutInflater mLayoutInflater;
    private int mSelectPos;

    public MenuAdpater(Context context , List<MenuModel> menuModelList, boolean isLeft){
        this.mContext = context;
        this.mMenuModeList = menuModelList;
        this.mIsLeft = isLeft;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    public int getSelectionPos(){
        return this.mSelectPos;
    }

    public void setSelectPos(int pos){
        this.mSelectPos = pos;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mMenuModeList!=null){
            return mMenuModeList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mMenuModeList!=null){
            return mMenuModeList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder = null;
        if(convertView==null){
            convertView = mLayoutInflater.inflate(R.layout.item_menu_view,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        }else{
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        MenuModel menuMode = mMenuModeList.get(position);
        myViewHolder.nameTx.setText(menuMode.mValue);
        int icon =0;
        if(position == mSelectPos){
            myViewHolder.nameTx.setTextColor(mContext.getResources().getColor(R.color.orange));
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.color_white_normal));
            if(menuMode.hasChild()){
                icon=R.drawable.icon_chose_arrow_sel;
            }
        }else{
            myViewHolder.nameTx.setTextColor(mContext.getResources().getColor(R.color.gray_80));
            if(menuMode.hasChild()){
                icon = R.drawable.icon_chose_arrow_nor;
            }
        }
        myViewHolder.nameTx.setCompoundDrawablesWithIntrinsicBounds(0,0,icon,0);
        return convertView;
    }

    class MyViewHolder{
        TextView nameTx;
        public MyViewHolder(View view){
            if(view!=null){
                nameTx = (TextView)view.findViewById(R.id.tx_menu_name);
            }
        }
    }
}
